$(function() {
	//设置活动按钮
	changeHeaderMenu(0);
	changeMenu(0, 6);
	// 初始化
	emptyForm();
	my_bind_check_num($('#giveTime'));
});

function emptyForm() {
	$('#deviceSn').val('');
	$('#deviceSn2').val('');
	$('#giveTime').val(0);
	$('#deviceSnChange').val('');
	$('#deviceSnChange2').val('');
	$('#deviceSnEdit').val('');
	$('#expireTime').val('');
}

function sure_add() {
	var deviceSn = $('#deviceSn').val();
	var deviceSn2 = $('#deviceSn2').val();
	if(!deviceSn || !deviceSn2) {
		layer.alert('请输入设备SN！', {icon: 2});
		return;
	}
	if(deviceSn != deviceSn2) {
		layer.alert('两次输入的设备SN不一致！', {icon: 2});
		return;
	}
	var auzTime = $('#auzTime').val();
	var auzTime2 = $('#auzTime2').val();
	if(!auzTime || !auzTime2) {
		layer.alert('请选择授权时间！', {icon: 2});
		return;
	}
	if(auzTime != auzTime2) {
		layer.alert('两次选择的授权时间不一致！', {icon: 2});
		return;
	}
	var giveTime = $('#giveTime').val();
	if(giveTime && (giveTime < 0 || giveTime > 30)) {
		layer.alert('赠送时间必须为0-30天！', {icon: 2});
		return;
	}
	add();
}

var adding = false;
function add() {
	if(adding)
		return;
	adding = true;
	var deviceModel = $('#deviceModel').val();
	var deviceSn = $('#deviceSn').val();
	var auzTime = $('#auzTime').val();
	var giveTime = $('#giveTime').val();
    data = {
    	model: deviceModel,
    	sn: deviceSn,
    	auzTime: auzTime,
    	giveTime: giveTime
    };
    $.getJSON('/fw/agent/deviceAuz_add.action', data, function(data){
		if(data.success && data.result){
			emptyForm();
			layer.alert(data.result.errors?data.result.errors:'授权成功！', {icon: 1});
		} else {
			layer.alert(data.result.errors?data.result.errors:'授权失败！', {icon: 2});
		}
		adding = false;
	});
}

function sure_change() {
	var deviceSn = $('#deviceSnChange').val();
	var deviceSn2 = $('#deviceSnChange2').val();
	if(!deviceSn || !deviceSn2) {
		layer.alert('请输入设备SN！', {icon: 2});
		return;
	}
	if(deviceSn == deviceSn2) {
		layer.alert('两次输入的设备SN不能一致！', {icon: 2});
		return;
	}
	change();
}

var changeing = false;
function change() {
	if(changeing)
		return;
	changeing = true;
	var deviceSn = $('#deviceSnChange').val();
	var deviceSn2 = $('#deviceSnChange2').val();
    data = {
    	sn: deviceSn,
    	sn2: deviceSn2
    };
    $.getJSON('/fw/agent/deviceAuz_change.action', data, function(data){
		if(data.success && data.result){
			emptyForm();
			layer.alert('授权转移成功！', {icon: 1});
		} else {
			layer.alert(data.result.errors?data.result.errors:'授权转移失败！', {icon: 2});
		}
		changeing = false;
	});
}

function sure_find() {
	var deviceSn = $('#deviceSnEdit').val();
	if(!deviceSn) {
		layer.alert('请输入需要更改的设备SN！', {icon: 2});
		return;
	}
    data = {
    	sn: deviceSn,
    };
    $.getJSON('/fw/agent/deviceAuz_getexpiretime.action', data, function(data){
		if(data.success && data.result){
			$('#expireTime').val(data.result.substring(0, 19));
		} else {
			layer.alert(data.result.errors?data.result.errors:'查询失败！', {icon: 2});
		}
	});
}

function sure_edit() {
	var deviceSn = $('#deviceSnEdit').val();
	if(!deviceSn) {
		layer.alert('请输入需要更改的设备SN！', {icon: 2});
		return;
	}
	var expireTime = $('#expireTime').val();
	if(!expireTime) {
		layer.alert('请选择到期时间！', {icon: 2});
		return;
	}
	edit();
}

var editing = false;
function edit() {
	if(editing)
		return;
	editing = true;
	var deviceSn = $('#deviceSnEdit').val();
	var expireTime = $('#expireTime').val();
    data = {
    	sn: deviceSn,
    	expireTime: expireTime
    };
    $.getJSON('/fw/agent/deviceAuz_setexpiretime.action', data, function(data){
		if(data.success && data.result){
			emptyForm();
			layer.alert('授权更改成功！', {icon: 1});
		} else {
			layer.alert(data.result.errors?data.result.errors:'授权更改失败！', {icon: 2});
		}
		editing = false;
	});
}